<template>
  <el-dialog title="安全事件研判" :close-on-click-modal="false" :visible.sync="visible" ref="myDialog"
    class="JNPF-dialog JNPF-dialog_center" lock-scroll width="1024px" :modal-append-to-body="false" append-to-body>
    <el-form :model="form" ref="formData" label-width="80px">
        <el-form-item label="事件类型">
          <el-radio-group v-model="form.m_status" ref="statusRadioGroup">
            <el-radio :label="1">安全事件</el-radio>
            <el-radio :label="2">非安全事件</el-radio>
            <el-radio :label="0">待定</el-radio>
          </el-radio-group>
        </el-form-item>
        
        <el-form-item label="事件类型">
          <!-- <el-input disabled
              type="input"
              v-model="form.eventType"
              placeholder="输入事件类型"
            ></el-input> -->
          <el-select v-model="form.eventType" placeholder="请选择事件类型" disabled>
        
          </el-select>
        </el-form-item>
        <el-form-item label="事件等级">
          <el-select v-model="form.eventLevel" placeholder="请选择事件等级">
            <!-- 从远程接口获取的数据填充这里 -->
            <el-option label="一般" value="0"></el-option>
            <el-option label="较大" value="1"></el-option>
            <el-option label="重大" value="2"></el-option>
            <el-option label="特别重大" value="3"></el-option>
          </el-select>
        </el-form-item>
        
        <el-form-item label="研判详情">
          <el-input type="textarea" v-model="form.analysisDetail"></el-input>
        </el-form-item>
        
        <el-form-item label="处置建议">
          <el-input type="textarea" v-model="form.disposalSuggestion"></el-input>
        </el-form-item>
        
        <el-form-item label="附件上传">
          <!-- 使用 JNPFUploadFz 组件，确保正确引入并配置 -->
          <JNPFUploadFz v-model="form.attachment"></JNPFUploadFz>
        </el-form-item>

        <el-form-item label="处置进度">
          <el-progress :percentage="disposalProgress" />
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="handleBtnClose">取消</el-button>
        <el-button type="primary" @click="handleSubmit">提交</el-button>
      </div>
  </el-dialog>
</template>

<script>
import request from '@/utils/request'
import { deepClone } from '@/utils'
export default {
  data() {
    return {
      visible: false,
      modelId:'',
      bizTableName:'',
      updateStatusApiId:'',
      ids:[],
      sendFunc:()=>{},
      disposalProgress: 0, // 进度初始化为0
      progressProportion:1,
      doneNum:0,
      sourceType:'关基',
      form: {
        m_status: 0,
        eventType: '',
        eventLevel: '0',
        analysisDetail: '',
        disposalSuggestion: '',
        attachment: null
      }
    };
  },
  methods: {
    init(modelId,data) {
      let {bizTableName,updateStatusApiId,ids,issueType,sourceType,sendFunc} = data
    
      debugger
      if(!modelId||!bizTableName||!updateStatusApiId|!issueType||!ids||ids.length<1){
        this.$message({
          type: 'error',
          message: '参数传入错误，请与系统管理员联系！',
          duration: 1500,
        })
        return
      }
      if (!sendFunc||typeof sendFunc != 'function') {
        this.$message({
          type: 'error',
          message: '参数传入错误2，请与系统管理员联系！',
          duration: 1500,
        })
        this.$refs.statusRadioGroup.focus()
        return
      }
      this.sourceType = sourceType
      this.sendFunc = sendFunc
      this.form.m_status = 0
      this.form.eventType = issueType
      this.modelId = modelId
      this.bizTableName = bizTableName
      this.updateStatusApiId = updateStatusApiId
      this.ids = ids
      this.disposalProgress = 0
      this.progressProportion = 100/this.ids.length
      this.doneNum = 0;
      this.visible = true
    },
    handleBtnClose(){
        // 重置表单数据
        this.$refs.formData.resetFields();
        this.visible = false;
        this.$emit('refreshDataList', true)
    },
    handleClose() {
      this.doneNum++
      this.disposalProgress = Math.round(this.doneNum * this.progressProportion)
      if(this.doneNum >= this.ids.length){
        // 重置表单数据
        this.$refs.formData.resetFields();
        this.visible = false;
        this.$emit('refreshDataList', true)
      }
    },
    handleSubmit() {
      // 在这里执行提交逻辑，可以调用远程接口等
      // this.$message.info(''+this.ids)
      // console.log('Form Submitted:', this.form);
      debugger
      if(this.form.m_status===0){
        this.$message({
          type: 'error',
          message: '请选择安全事件或非安全事件',
          duration: 1500,
        })
        return
      }
      this.doneNum = 0;
      if(this.form.m_status===2){ //非安全事件
        this.ids.forEach(id=>{
          let apiUrl = '/api/system/DataInterface/'+this.updateStatusApiId+'/Actions/Call'
          let params = {
              "tenant_id": "",
              "paramList": [
                  {
                      "field": "tableName", "value": this.bizTableName
                  },
                  {
                      "field": "id", "value": id
                  },
                  {
                      "field": "status", "value": '2'
                  }
              ]
          }
          request(
          {
            url: apiUrl,
            method: 'post',
            data:params
          }).then(res2 => {
            this.handleClose();
          }).catch(res2=>{
            this.handleClose();
          })
        })
        return
      }

      let modelUrl = '/api/visualdev/OnlineDev/'+this.modelId
      this.ids.forEach(id=>{
        let url = modelUrl + '/' + id
        request(
        {
          url: url,
          method: 'get'
        }).then(res => {
          // console.log('res', JSON.stringify(res));
          let data = JSON.parse(res.data.data)
          if(data.m_status==0){
            debugger
            data.id = res.data.id
            data.m_recipient_level = this.form.eventType
            if(this.sourceType==='flowlog'){
              data.m_recipient_level = data.r_type_name
            }else if(this.sourceType==='weblog'){
              data.m_recipient_level = data.logtype
            }else if(this.sourceType==='assetlog'){
              data.m_recipient_level = data.logType
            }
            data.m_response_level = this.form.eventLevel
            data.m_descript = this.form.analysisDetail
            data.m_disposalsuggestions = this.form.disposalSuggestion
            data.m_attachment = this.form.attachment
            this.sendFunc(data,res1=>{
              let apiUrl = '/api/system/DataInterface/'+this.updateStatusApiId+'/Actions/Call'
              let params = {
                  "tenant_id": "",
                  "paramList": [
                      {
                          "field": "tableName", "value": this.bizTableName
                      },
                      {
                          "field": "id", "value": id
                      },
                      {
                          "field": "status", "value": '1'
                      }
                  ]
              }
              request(
              {
                url: apiUrl,
                method: 'post',
                data:params
              }).then(res2 => {
                this.handleClose();
              }).catch(res2=>{
                this.handleClose();
              })
            })
          }else{
            this.handleClose();
          }
        }).catch(res=>{
          this.handleClose();
          console.error('err', res);
        })
      })
    },
  }
};
</script>
<style lang="scss" scoped>

</style>